<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class wxapi_task
{
    function post_install()
    {   //初始化logo
        $this->__initLogo();
        //初始化banner
        $this->__initBanner();
        //初始化导航栏
        $this->__initNavigator();
        //初始化标签
        $this->__initTagGoogs();
        //初始化法律条款
        $this->__initLaws();
        //初始化分类页
        $this->__initClassIfy();

    }//End Function

    public function post_update()
    {
        //初始化logo
        $this->__initLogo();
        //初始化banner
        $this->__initBanner();
        //初始化导航栏
        $this->__initNavigator();
        //初始化标签
        $this->__initTagGoogs();
        //初始化法律条款
        $this->__initLaws();
        //初始化分类页
        $this->__initClassIfy();
    }//End Function

    public function post_uninstall()
    {
        app::get('wxapi')->setConf('wxapps.laws',[]);
        app::get('wxapi')->setConf('wxapps.navigator',[]);
        app::get('wxapi')->setConf('wxapps.logo','');
        $_sql = "DELETE FROM sdb_desktop_tag WHERE tag_type='goods';";
        kernel::database()->exec($_sql);
        $_sql = "DELETE FROM sdb_b2c_goods_tags;";
        kernel::database()->exec($_sql);
        //sdb_site_smallprogram_columns
        $_sql = "DELETE FROM sdb_site_smallprogram_columns;";
        kernel::database()->exec($_sql);

        app::get('wxapi')->setConf('wxapps.classify',[]);

    }//End Function

    //初始化logo
    private function __initLogo()
    {
        logger::info('Initial wxapi logo...');
        $app_dir = app::get('wxapi')->app_dir;
        $logoIdet = app::get('wxapi')->getConf('wxapps.logo');
        $obj_image = app::get('image')->model('image');
        $logoIdet = $logoIdet ? $logoIdet : $obj_image->gen_id();
        $image_id = $obj_image->store($app_dir.'/initial/images/logo.png',$logoIdet);
        app::get('wxapi')->setConf('wxapps.logo',$image_id);
    }//End Function

    //初始化banner
    private function __initBanner()
    {
        logger::info('Initial wxapi banner...');
        $app_dir = app::get('wxapi')->app_dir;
        $obj_image = app::get('image')->model('image');
        //第一张banner
        $image_id = $obj_image->store($app_dir.'/initial/images/banner-02.jpg',$obj_image->gen_id());
        $_sql = "insert into `sdb_site_smallprogram_columns` ( `title`, `custom_url`, `images`, `tostr`, `display_order`, `base`, `location`, `is_open`) values ( '导航BANNER01', 'goods_detail?id=1', '{$image_id}', '', '0', 'index', 'banner', 'true');";
        kernel::database()->exec($_sql);
        //第二张banner
        $image_id = $obj_image->store($app_dir.'/initial/images/banner-03.jpg',$obj_image->gen_id());
        $_sql = "insert into `sdb_site_smallprogram_columns` ( `title`, `custom_url`, `images`, `tostr`, `display_order`, `base`, `location`, `is_open`) values ( '导航BANNER02', 'goods_detail?id=2', '{$image_id}', '', '0', 'index', 'banner', 'true');";
        kernel::database()->exec($_sql);
    }

    //初始化导航栏
    private function __initNavigator()
    {
        logger::info('Initial wxapi navigator...');
        $list = array(
            array(
                'code'      =>'2',
                'type'      =>'cat',
                'name'      =>'T恤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'4',
                'type'      =>'cat',
                'name'      =>'连衣裙',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'3',
                'type'      =>'cat',
                'name'      =>'衬衫',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/aa/49/4d/60718c6bf367f48e248e46c743bfccac86f1cd8a.jpg?1528357974#h',
            ),
            array(
                'code'      =>'9',
                'type'      =>'tag',
                'name'      =>'时尚基本款',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'5',
                'type'      =>'cat',
                'name'      =>'半身裙',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/3e/1d/15/c167bc23478965381fe37f63375bb132cb24da53.jpg?1528358094#h',
            ),
            array(
                'code'      =>'26,60',
                'type'      =>'cat',
                'name'      =>'短裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/3c/da/1b/aaf2e63b05f41a90f31c1ffadce4c502cb8ed712.jpg?1528371136#h',
            ),
            array(
                'code'      =>'53,27',
                'type'      =>'cat',
                'name'      =>'休闲长裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'60,64',
                'type'      =>'cat',
                'name'      =>'牛仔裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
        );
        app::get('wxapi')->setConf('wxapps.navigator',$list);
    }//End Function

    //首页标签商品
    private function __initTagGoogs()
    {
        logger::info('Initial wxapi TagGoogs...');
        //第一个初始标签-本季新品
        $_sql = "insert into `sdb_desktop_tag` ( `tag_name`, `tag_mode`, `app_id`, `tag_type`, `tag_abbr`, `display_order`, `tag_bgcolor`, `tag_fgcolor`, `show_small_tag`, `show_small_tag_goods`, `link_switch`, `tag_link`, `tag_filter`, `rel_count`) 
                  values ( '本季新品', 'normal', 'b2c', 'goods', 'BEST SALE', '0', '#eeeeee', '#333333', '0', '1', '0', '#', '', '0');";
        kernel::database()->exec($_sql);
        $tagId = kernel::database()->lastinsertid();
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '1', '446d24eae9b2a55cb65f76e15c69e417');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '2', 'b0fb7b2693aeeafa4818f5df3b7078eb');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '3', '5a140e675d0a9414bca9b78ab64a64c7');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '4', '116240f1715cd583ee0f5495da8a4203');";
        kernel::database()->exec($tag_sql);
        //第二个初始标签-人气热销
        $_sql = "insert into `sdb_desktop_tag` ( `tag_name`, `tag_mode`, `app_id`, `tag_type`, `tag_abbr`, `display_order`, `tag_bgcolor`, `tag_fgcolor`, `show_small_tag`, `show_small_tag_goods`, `link_switch`, `tag_link`, `tag_filter`, `rel_count`) 
                  values ( '人气热销', 'normal', 'b2c', 'goods', 'BEST SALE', '0', '#eeeeee', '#333333', '0', '1', '0', '#', '', '0');";
        kernel::database()->exec($_sql);
        $tagId = kernel::database()->lastinsertid();
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '5', '219b69197cb2a4cd6e319f89e3fa83c6');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '6', '0a0131a306f38bd2c99cdb3f4e3a7d8c');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '7', '3c4d1a05d0a164688a12cd744aad48dc');";
        kernel::database()->exec($tag_sql);
        $tag_sql = "insert into `sdb_b2c_goods_tags` ( `tags_id`, `goods_id`, `image`) values ( '{$tagId}', '8', '3713985caa3797c94492f0483ec2e64b');";
        kernel::database()->exec($tag_sql);
    }//End Function

    //初始化法律条款
    private function __initLaws()
    {
        logger::info('Initial wxapi Laws...');
        $laws = array(
            'terrace' => 'ECStore测试平台',
            'company' => 'ShopEX测试有限公司',
            'effectivetime' => '2028-03-30',
        );
        app::get('wxapi')->setConf('wxapps.laws',$laws);
    }

    //初始化分类页
    private function __initClassIfy()
    {
        logger::info('Initial wxapi ClassIfy...');
        $apple = array();
        $apple['item_name'] = '推荐专区';
        $apple['children'] = array(
            array(
                'code'      =>'2',
                'name'      =>'T恤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'4',
                'name'      =>'连衣裙',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'3',
                'name'      =>'衬衫',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/aa/49/4d/60718c6bf367f48e248e46c743bfccac86f1cd8a.jpg?1528357974#h',
            ),
            array(
                'code'      =>'9',
                'name'      =>'时尚基本款',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'5',
                'name'      =>'半身裙',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/3e/1d/15/c167bc23478965381fe37f63375bb132cb24da53.jpg?1528358094#h',
            ),
            array(
                'code'      =>'26,60',
                'name'      =>'短裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/3c/da/1b/aaf2e63b05f41a90f31c1ffadce4c502cb8ed712.jpg?1528371136#h',
            ),
            array(
                'code'      =>'53,27',
                'name'      =>'休闲长裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
            array(
                'code'      =>'60,64',
                'name'      =>'牛仔裤',
                'thumLogo'  =>'https://cacheimage.ishopex.cn/public/images/72/bc/94/cdf0f83905cbf8a295372988776670b3254bbd8d.jpg?1528357957#h',
            ),
        );
        $stairlist = app::get('desktop')->model('tag')->getList('tag_id',array('tag_type'=>'goods'),0,2);
        $stairlist && ($stairlist = array_column($stairlist,'tag_id'));
        $apple['stairlist'] = $stairlist;
        app::get('wxapi')->setConf('wxapps.classify',$apple);

    }

}//End Class
